package com.xnyzc.lhy.mis.service.impl.cms.driver;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xnyzc.lhy.common.component.convert.DateToStringConverter;
import com.xnyzc.lhy.common.entity.Qw;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.penum.driver.EAccountStatusType;
import com.xnyzc.lhy.common.penum.wallet.EIsTransaction;
import com.xnyzc.lhy.common.penum.wallet.ERecordType;
import com.xnyzc.lhy.common.util.*;
import com.xnyzc.lhy.mis.entity.area.OaSysArea;
import com.xnyzc.lhy.mis.entity.cms.driver.DriverUserInfo;
import com.xnyzc.lhy.mis.entity.driver.wallet.OaDBandSupplierSeparateAccount;
import com.xnyzc.lhy.mis.entity.driver.wallet.OaDWalletRecord;
import com.xnyzc.lhy.mis.entity.param.cms.PageDTO;
import com.xnyzc.lhy.mis.entity.param.cms.PageVo;
import com.xnyzc.lhy.mis.entity.result.cms.driver.*;
import com.xnyzc.lhy.mis.entity.result.user.DriverUserResult;
import com.xnyzc.lhy.mis.entity.system.OaSysUserSeparateAccount;
import com.xnyzc.lhy.mis.entity.user.OaDDriverCash;
import com.xnyzc.lhy.mis.entity.user.OaDUser;
import com.xnyzc.lhy.mis.entity.user.OaDUserInfo;
import com.xnyzc.lhy.mis.mapper.area.OaSysAreaMapper;
import com.xnyzc.lhy.mis.mapper.driver.wallet.OaDWalletMapper;
import com.xnyzc.lhy.mis.mapper.driver.wallet.OaDWalletRecordMapper;
import com.xnyzc.lhy.mis.mapper.order.OaDOrderMapper;
import com.xnyzc.lhy.mis.mapper.system.OaDBandSupplierSeparateAccountMapper;
import com.xnyzc.lhy.mis.mapper.system.OaSysUserSeparateAccountMapper;
import com.xnyzc.lhy.mis.mapper.user.OaDDriverCashMapper;
import com.xnyzc.lhy.mis.mapper.user.OaDUserInfoMapper;
import com.xnyzc.lhy.mis.mapper.user.OaDUserMapper;
import com.xnyzc.lhy.mis.mapper.user.OaDUserOfficeMapper;
import com.xnyzc.lhy.mis.mapper.vehicle.OaSysVehicleMapper;
import com.xnyzc.lhy.mis.service.cms.driver.CmsDriverWalletManageService;
import com.xnyzc.lhy.mis.service.cms.util.IOaSysGetOfficeIdListService;
import com.xnyzc.lhy.mis.entity.driver.wallet.OaDWallet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 司机管理接口实现类
 *
 * @author zhaolijie
 * @date 2019-08-21 19:37
 */

@Service
@Transactional
public class CmsDriverWalletManageServiceImpl implements CmsDriverWalletManageService {

    @Autowired
    private IOaSysGetOfficeIdListService iOaSysGetOfficeIdListService;

    @Autowired
    private OaDWalletMapper oadWalletMapper;

    @Autowired
    private OaSysAreaMapper oaSysAreaMapper;

    @Autowired
    private OaDWalletRecordMapper oaDWalletRecordMapper;

    @Autowired
    private OaDUserMapper oaDUserMapper;

    @Autowired
    private OaDUserOfficeMapper oaDUserOfficeMapper;

    @Autowired
    private OaDUserInfoMapper oaDUserInfoMapper;

    @Autowired
    private OaSysUserSeparateAccountMapper oaSysUserSeparateAccountMapper;

    @Autowired
    private OaDBandSupplierSeparateAccountMapper oaDBandSupplierSeparateAccountMapper;

    @Autowired
    private OaSysVehicleMapper oaSysVehicleMapper;

    @Autowired
    private OaDOrderMapper oaDOrderMapper;

    @Autowired
    private OaDDriverCashMapper oaDDriverCashMapper;


    public static JSONArray getCityInfo(JSONArray classACity, JSONArray classBCity) {
        for (int i = 0; i < classBCity.size(); i++) {
            JSONObject classBCityJsonObject = classBCity.getJSONObject(i);
            for (int j = 0; j < classACity.size(); j++) {
                JSONObject classACityJsonObject = classACity.getJSONObject(j);
                if (classBCityJsonObject.getString("parentId").equals(classACityJsonObject.getString("areaId"))) {
                    classBCityJsonObject.put("province", classACityJsonObject.getString("name"));
                    classBCityJsonObject.put("city", classBCityJsonObject.getString("name"));
                    break;
                }
            }
        }
        return classBCity;
    }

    @Override
    public Rv getTransferHistoryRecordList() {
        //
        List<CmsTransferHistoryRes> cmsTransferHistoryResList =  oaDDriverCashMapper.selectTransferHistoryRecordList();
        for (CmsTransferHistoryRes ctfh : cmsTransferHistoryResList
             ) {
            List<CmsTransferHistoryChildRes> cmsTransferHistoryChildRes = oaDDriverCashMapper.selectTransferHistoryChildList(ctfh.getBatchNo());
            ctfh.setTransferHistoryChildResList(cmsTransferHistoryChildRes);
        }
        return Rv.wrap(cmsTransferHistoryResList);
    }

    /**
     * 获取司机列表
     *
     * @param cmsOaDriverWalletListRes
     * @return
     */
   /* @Override
    public Rv getCmsDriverWalletList(CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        QueryWrapper<OaSysArea> areaQueryWrapper = new QueryWrapper<>();
        List<OaSysArea> areaList = oaSysAreaMapper.selectList(areaQueryWrapper);
        PageDTO dto = new PageDTO();
        dto.setPage(Long.valueOf(cmsOaDriverWalletListRes.getCurrent()));
        dto.setLimit(Long.valueOf(cmsOaDriverWalletListRes.getPageSize()));
        IPage iPage = new Page();
        iPage.setCurrent(cmsOaDriverWalletListRes.getCurrent());
        iPage.setSize(cmsOaDriverWalletListRes.getPageSize());
        Integer registerCityId;
        if (CheckUtil.objIsNotEmpty(cmsOaDriverWalletListRes.getRegisterCityId())) {
            registerCityId = Integer.parseInt(cmsOaDriverWalletListRes.getRegisterCityId());
        } else {
            registerCityId = null;
        }
        List<DriverUserInfo> driverWalletList = oaDUserMapper.getDriverWalletManagementList(dto.getIndex(), dto.getLimit(), cmsOaDriverWalletListRes.getDriverPhone(),
                cmsOaDriverWalletListRes.getDriverName(), registerCityId);
        Integer count = oaDUserMapper.getDriverWalletManagementListCount(cmsOaDriverWalletListRes.getDriverPhone(), cmsOaDriverWalletListRes.getDriverName());
        iPage.setTotal(count);
        BigDecimal zero = new BigDecimal(0);
        List<Long> userList = new ArrayList<>();
        // 总计
        List<DriverUserInfo> driverWalletListSum = oaDUserMapper.getDriverWalletManagementListSum();
        for (DriverUserInfo info : driverWalletListSum) {
            userList.add(info.getDriverId());
        }
        // 合计可提现金额
        CmsOaDriverWalletListRes cashBanlanceSum = oadWalletMapper.getCashBanlanceSum(TokenUtil.getTag(), userList);
        // 合计提现中金额
        CmsOaDriverWalletListRes cashingBanlanceSum = oadWalletMapper.getCashingBanlanceSum(TokenUtil.getTag(), userList);
        // 合计冻结金额
        CmsOaDriverWalletListRes frozenBanlanceSum = oadWalletMapper.getFrozenBanlanceSum(TokenUtil.getTag(), userList);
        JSONObject resultJsonObject = new JSONObject();
        resultJsonObject.put("iPage", iPage);
        if (CheckUtil.objIsNotEmpty(cashBanlanceSum)) {
            resultJsonObject.put("cashBanlanceSum", cashBanlanceSum.getCashBanlance());
        } else {
            resultJsonObject.put("cashBanlanceSum", zero);
        }
        if (CheckUtil.objIsNotEmpty(cashingBanlanceSum)) {
            resultJsonObject.put("cashingBanlanceSum", cashingBanlanceSum.getCashingBanlance());
        } else {
            resultJsonObject.put("cashingBanlanceSum", zero);
        }
        if (CheckUtil.objIsNotEmpty(frozenBanlanceSum)) {
            resultJsonObject.put("frozenBanlanceSum", frozenBanlanceSum.getFrozenBalance());
        } else {
            resultJsonObject.put("frozenBanlanceSum", zero);
        }
        if (driverWalletList.isEmpty()) {
            iPage.setRecords(driverWalletList);
            resultJsonObject.put("iPage", iPage);
            return Rv.wrap(EErrorCode.success, resultJsonObject);
        }
        List<Long> userIdList = new ArrayList<>();
        for (DriverUserInfo res : driverWalletList) {
            userIdList.add(res.getDriverId());
            if (CheckUtil.objIsNotEmpty(res.getAccountStatus())) {
                res.setAccountStatusStr(EAccountStatusType.findById(res.getAccountStatus()).getValue());
            }
            for (OaSysArea area : areaList) {
                if (CheckUtil.objIsNotEmpty(res.getDriverCityId())) {
                    if (res.getDriverCityId().equals(area.getAdCode())) {
                        res.setCityName(area.getName());
                        break;
                    }
                }
            }
        }
        QueryWrapper<OaDWallet> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(OaDWallet.DRIVER_ID, userIdList);
        List<OaDWallet> dWalletList = oadWalletMapper.selectList(queryWrapper);
        if (!dWalletList.isEmpty()) {
            for (DriverUserInfo res : driverWalletList) {
                for (OaDWallet wallet : dWalletList) {
                    if (res.getDriverId().equals(wallet.getDriverId())) {
                        res.setWalletBanlance(wallet.getWalletBalance());
                        res.setCashingBanlance(wallet.getCashingBalance());
                        res.setCashBanlance(wallet.getCashBalance());
                        res.setFrozenBalance(wallet.getFrozenBalance());
                        res.setCashTotal(wallet.getCashTotal());
                    }
                }
            }
        }
        iPage.setRecords(driverWalletList);
        return Rv.wrap(EErrorCode.success, resultJsonObject);
    }
*/
    @Override
    public Rv getCmsDriverWalletList(CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        QueryWrapper<OaSysArea> areaQueryWrapper = new QueryWrapper<>();
        List<OaSysArea> areaList = oaSysAreaMapper.selectList(areaQueryWrapper);
        PageDTO dto = new PageDTO();
        dto.setPage(Long.valueOf(cmsOaDriverWalletListRes.getCurrent()));
        dto.setLimit(Long.valueOf(cmsOaDriverWalletListRes.getPageSize()));
        IPage iPage = new Page();
        iPage.setCurrent(cmsOaDriverWalletListRes.getCurrent());
        iPage.setSize(cmsOaDriverWalletListRes.getPageSize());
        Long officeId;
        if (cmsOaDriverWalletListRes.getOfficeId() == "" || CheckUtil.strIsEmpty(cmsOaDriverWalletListRes.getOfficeId())) {
            officeId = null;
        } else {
            officeId = Long.valueOf(cmsOaDriverWalletListRes.getOfficeId());
        }
        if (cmsOaDriverWalletListRes.getOfficeId() == "" || CheckUtil.strIsEmpty(cmsOaDriverWalletListRes.getOfficeId())) {
            cmsOaDriverWalletListRes.setOfficeId("0");
        }
        List<DriverUserResult> driverAccountList = oaDUserMapper.getDriverAccountManageMentList(dto.getIndex(), dto.getLimit(), cmsOaDriverWalletListRes.getDriverPhone(),
                cmsOaDriverWalletListRes.getBankAccountName(), officeId);
        Integer count = oaDUserMapper.getDriverWalletManagementListCount(cmsOaDriverWalletListRes.getDriverPhone(), cmsOaDriverWalletListRes.getBankAccountName(), officeId);
        iPage.setTotal(count);
        BigDecimal zero = new BigDecimal(0);
        List<Long> userList = new ArrayList<>();
        // 总计
        List<DriverUserInfo> driverWalletListSum = oaDUserMapper.getDriverWalletManagementListSum();
        for (DriverUserInfo info : driverWalletListSum) {
            userList.add(info.getDriverId());
        }
        // 合计可提现金额
        CmsOaDriverWalletListRes cashBanlanceSum = oadWalletMapper.getCashBanlanceSum(TokenUtil.getTag(), userList);
        // 合计提现中金额
        CmsOaDriverWalletListRes cashingBanlanceSum = oadWalletMapper.getCashingBanlanceSum(TokenUtil.getTag(), userList);
        // 合计冻结金额
        CmsOaDriverWalletListRes frozenBanlanceSum = oadWalletMapper.getFrozenBanlanceSum(TokenUtil.getTag(), userList);
        JSONObject resultJsonObject = new JSONObject();
        resultJsonObject.put("iPage", iPage);
        if (CheckUtil.objIsNotEmpty(cashBanlanceSum)) {
            resultJsonObject.put("cashBanlanceSum", cashBanlanceSum.getCashBanlance());
        } else {
            resultJsonObject.put("cashBanlanceSum", zero);
        }
        if (CheckUtil.objIsNotEmpty(cashingBanlanceSum)) {
            resultJsonObject.put("cashingBanlanceSum", cashingBanlanceSum.getCashingBanlance());
        } else {
            resultJsonObject.put("cashingBanlanceSum", zero);
        }
        if (CheckUtil.objIsNotEmpty(frozenBanlanceSum)) {
            resultJsonObject.put("frozenBanlanceSum", frozenBanlanceSum.getFrozenBalance());
        } else {
            resultJsonObject.put("frozenBanlanceSum", zero);
        }
        if (driverAccountList.isEmpty()) {
            iPage.setRecords(driverAccountList);
            resultJsonObject.put("iPage", iPage);
            return Rv.wrap(EErrorCode.success, resultJsonObject);
        }
        List<Long> userIdList = new ArrayList<>();
        for (DriverUserResult res : driverAccountList) {
            userIdList.add(res.getDriverId().longValue());
        }
        QueryWrapper<OaDWallet> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(OaDWallet.DRIVER_ID, userIdList);
        List<OaDWallet> dWalletList = oadWalletMapper.selectList(queryWrapper);
        if (!dWalletList.isEmpty()) {
            for (DriverUserResult res : driverAccountList) {
                for (OaDWallet wallet : dWalletList) {
                    if (res.getDriverId().equals(wallet.getDriverId())) {
                        res.setWalletBanlance(wallet.getWalletBalance());
                        res.setCashingBanlance(wallet.getCashingBalance());
                        res.setCashBanlance(wallet.getCashBalance());
                        res.setFrozenBalance(wallet.getFrozenBalance());
                        res.setCashTotal(wallet.getCashTotal());
                    }
                }
            }
        }
        iPage.setRecords(driverAccountList);
        return Rv.wrap(EErrorCode.success, resultJsonObject);
    }


    @Override
    public Rv getDriverBandSupplierSeparateAccountList(CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        JSONObject res = new JSONObject();
        Long driverId = cmsOaDriverWalletListRes.getDriverId();
        String registerCityName = cmsOaDriverWalletListRes.getRegisterCityName();
        if (CheckUtil.objIsEmpty(driverId, registerCityName)) {
            Rv.error(EErrorCode.missingArg);
        }
        //获取城市code
        QueryWrapper<OaSysArea> areaQueryWrapper = new QueryWrapper<>();
        areaQueryWrapper.eq(OaSysArea.NAME, registerCityName);
        List<OaSysArea> areaList = oaSysAreaMapper.selectList(areaQueryWrapper);
        if (areaList.size() <= 0) {
            Rv.error(EErrorCode.cityNotFound);
        }
        Integer cityCode = Integer.parseInt(areaList.get(0).getAdCode());
        //获取司机的部门id
//        QueryWrapper<OaDUserOffice> officeQueryWrapper = new QueryWrapper<>();
//        officeQueryWrapper.eq(OaDUserOffice.DRIVER_ID,driverId);
//        List<OaDUserOffice> oaDUserOfficeList = oaDUserOfficeMapper.selectList(officeQueryWrapper);
//        Long officeId = oaDUserOfficeList.get(0).getOfficeId();
        //获取供应商的分账管理表
        QueryWrapper<OaSysUserSeparateAccount> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq(OaSysUserSeparateAccount.CITY_ID, cityCode);
//        accountQueryWrapper.eq(OaSysUserSeparateAccount.OFFICE_ID,officeId);
        accountQueryWrapper.eq(OaSysUserSeparateAccount.DELETED, 0);
        List<OaSysUserSeparateAccount> oaSysUserSeparateAccountList = oaSysUserSeparateAccountMapper.selectList(accountQueryWrapper);
        res.put("supplierSeparateAccountList", oaSysUserSeparateAccountList);
        //获取司机绑定分账信息
        List<OaDBandSupplierSeparateAccount> oaDBandSupplierSeparateAccountList = oaDBandSupplierSeparateAccountMapper.selectList(Qw.create()
                .eq(OaDBandSupplierSeparateAccount.DRIVER_ID, driverId)
                .eq(OaDBandSupplierSeparateAccount.DELETED, 0));
        res.put("DBandSupplierSeparateAccountList", oaDBandSupplierSeparateAccountList);
        return Rv.wrap(res);
    }

    @Override
    public Rv driverBandSupplierSeparateAccount(List<OaDBandSupplierSeparateAccount> list) {
        Long userId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(userId)) {
            Rv.error(EErrorCode.noAuth);
        }
        if (CheckUtil.objIsEmpty(list)) {
            Rv.error(EErrorCode.missingArg);
        }
        for (OaDBandSupplierSeparateAccount info : list) {
            Long driverBandId = info.getDriverBandId();
            if (driverBandId == null) {
                //验证是否存在
                List<OaDBandSupplierSeparateAccount> oaDBandSupplierSeparateAccountList = oaDBandSupplierSeparateAccountMapper.selectList(Qw.create()
                        .eq(OaDBandSupplierSeparateAccount.DRIVER_ID, info.getDriverId())
                        .eq(OaDBandSupplierSeparateAccount.CITY_ID, info.getCityId())
                        .eq(OaDBandSupplierSeparateAccount.CHANNEL_ID, info.getChannelId())
                        .eq(OaDBandSupplierSeparateAccount.DELETED, 0));
                if (oaDBandSupplierSeparateAccountList.size() > 0) {
                    Rv.error(EErrorCode.supplierSeparateAccountIsOne);
                }
                OaDBandSupplierSeparateAccount newInfo = new OaDBandSupplierSeparateAccount();
                newInfo.setDriverBandId(IDUtil.nextId());
                newInfo.setDriverId(info.getDriverId());
                newInfo.setChannelId(info.getChannelId());
                newInfo.setCityId(info.getCityId());
                newInfo.setChannelProportion(info.getChannelProportion());
                newInfo.setQilinProportion(info.getQilinProportion());
                newInfo.setCpProportion(info.getCpProportion());
                newInfo.setDriverViolationProportion(info.getDriverViolationProportion());
                newInfo.setDriverWithdrawalProportion(info.getDriverWithdrawalProportion());
                newInfo.setDeleted(0);
                newInfo.setCompanyId(info.getCompanyId());
                newInfo.setOfficeId(info.getOfficeId());
                newInfo.setCreateUser(userId);
                newInfo.setCreateTime(new Date());
                newInfo.setUpdateUser(userId);
                newInfo.setUpdateTime(new Date());
                newInfo.setSeparateAccountId(info.getSeparateAccountId());
                oaDBandSupplierSeparateAccountMapper.insert(newInfo);
            } else {
                OaDBandSupplierSeparateAccount newInfo = new OaDBandSupplierSeparateAccount();
                newInfo.setDriverBandId(driverBandId);
                newInfo.setDriverId(info.getDriverId());
                newInfo.setChannelId(info.getChannelId());
                newInfo.setCityId(info.getCityId());
                newInfo.setChannelProportion(info.getChannelProportion());
                newInfo.setQilinProportion(info.getQilinProportion());
                newInfo.setCpProportion(info.getCpProportion());
                newInfo.setDriverViolationProportion(info.getDriverViolationProportion());
                newInfo.setDriverWithdrawalProportion(info.getDriverWithdrawalProportion());
                newInfo.setDeleted(0);
                newInfo.setCompanyId(info.getCompanyId());
                newInfo.setOfficeId(info.getOfficeId());
                newInfo.setUpdateUser(userId);
                newInfo.setUpdateTime(new Date());
                newInfo.setSeparateAccountId(info.getSeparateAccountId());
                oaDBandSupplierSeparateAccountMapper.updateById(newInfo);
            }
        }
        return Rv.wrap();
    }

    private List<CmsOaDriverWalletListRes> getInfo(List<CmsOaDriverWalletListRes> cmsOaDriverWalletListRes) {
        List<String> classBCity = new ArrayList<>(cmsOaDriverWalletListRes.size());
        for (int i = 0; i < cmsOaDriverWalletListRes.size(); i++) {
            classBCity.add(cmsOaDriverWalletListRes.get(i).getRegisterCityId());
        }
        // 查询二级城市信息
        List<OaSysArea> classBCityMdict = this.findCityMdict(classBCity, OaSysArea.AD_CODE);
        List<String> classACity = new ArrayList<>(classBCityMdict.size());
        for (int i = 0; i < classBCityMdict.size(); i++) {
            classACity.add(classBCityMdict.get(i).getParentId());
        }
        // 查询一级城市信息
        List<OaSysArea> classAcityMdict = this.findCityMdict(classACity, OaSysArea.AREA_ID);

        // B级JSONArray
        JSONArray classBjsonArray = JSONArray.parseArray(JSON.toJSONString(classBCityMdict));
        // A级JSONArray
        JSONArray classAjsonArray = JSONArray.parseArray(JSON.toJSONString(classAcityMdict));
        JSONArray cityInfo = CmsDriverWalletManageServiceImpl.getCityInfo(classAjsonArray, classBjsonArray);
        for (int i = 0; i < cmsOaDriverWalletListRes.size(); i++) {
            for (int j = 0; j < cityInfo.size(); j++) {
                if (CheckUtil.objIsNotEmpty(cmsOaDriverWalletListRes.get(i).getRegisterCityId()) && CheckUtil.objIsNotEmpty(cityInfo.getJSONObject(j).getString("adCode"))) {
                    if (cmsOaDriverWalletListRes.get(i).getRegisterCityId().equals(cityInfo.getJSONObject(j).getString("adCode"))) {
                        if (CheckUtil.objIsNotEmpty(cityInfo.getJSONObject(j).getString("province"))) {
                            cmsOaDriverWalletListRes.get(i).setProvince(cityInfo.getJSONObject(j).getString("province"));
                        }
                        if (CheckUtil.objIsNotEmpty(cityInfo.getJSONObject(j).getString("city"))) {
                            cmsOaDriverWalletListRes.get(i).setCity(cityInfo.getJSONObject(j).getString("city"));
                        }
                    }
                }
            }
        }
        return cmsOaDriverWalletListRes;
    }

    /**
     * 导出司机信息
     *
     * @return
     */
    @Override
    public Rv exportGetWallet(CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        QueryWrapper<OaSysArea> areaQueryWrapper = new QueryWrapper<>();
        List<OaSysArea> areaList = oaSysAreaMapper.selectList(areaQueryWrapper);
        List<DriverUserInfo> driverWalletList = oaDUserMapper.exportDriverWalletManagementList(cmsOaDriverWalletListRes.getDriverPhone(), cmsOaDriverWalletListRes.getDriverName());
        List<Long> userIdList = new ArrayList<>();
        if (!driverWalletList.isEmpty()) {
            for (DriverUserInfo res : driverWalletList) {
                userIdList.add(res.getDriverId());
                for (OaSysArea area : areaList) {
                    if (CheckUtil.objIsNotEmpty(res.getDriverCityId())) {
                        if (res.getDriverCityId().equals(area.getAdCode())) {
                            res.setCityName(area.getName());
                            break;
                        }
                    }
                }
                if (CheckUtil.objIsNotEmpty(res.getAccountStatus())) {
                    res.setAccountStatusStr(EAccountStatusType.findById(res.getAccountStatus()).getValue());
                }
            }
        }
        QueryWrapper<OaDWallet> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(OaDWallet.DRIVER_ID, userIdList);
        List<OaDWallet> dWalletList = oadWalletMapper.selectList(queryWrapper);
        if (!dWalletList.isEmpty()) {
            for (DriverUserInfo res : driverWalletList) {
                for (OaDWallet wallet : dWalletList) {
                    if (res.getDriverId().equals(wallet.getDriverId())) {
                        res.setWalletBanlance(wallet.getWalletBalance());
                        res.setCashBanlance(wallet.getCashBalance());
                        res.setFrozenBalance(wallet.getFrozenBalance());
                        res.setCashTotal(wallet.getCashTotal());
                        res.setCashingBanlance(wallet.getCashingBalance());
                    }
                }
            }
        }
        return Rv.wrap(EErrorCode.success, driverWalletList);
    }


    /**
     * 查询城市字典
     *
     * @return
     */
    private List<OaSysArea> findCityMdict(List<String> inList, String findField) {
        //查询车字典数据
        QueryWrapper<OaSysArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(findField, inList);
        queryWrapper.select(OaSysArea.AREA_ID, OaSysArea.PARENT_ID, OaSysArea.NAME, OaSysArea.AD_CODE);
        return oaSysAreaMapper.selectList(queryWrapper);
    }

    /**
     * 详情页面中表格信息
     *
     * @return
     */
    @Override
    public Rv getCmsDriverWalletDetailList(CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        Integer current = cmsOaDriverWalletListRes.getCurrent();
        Integer pageSize = cmsOaDriverWalletListRes.getPageSize();
        Long driverId = cmsOaDriverWalletListRes.getDriverId();
        if (CheckUtil.objIsEmpty(driverId) || CheckUtil.objIsEmpty(current) || CheckUtil.objIsEmpty(pageSize)) {
            throw PangException.create(EErrorCode.missingArg);
        }
        List<OaDUserRechargeListRes> resultList = new ArrayList<>();
        PageVo pageVo = new PageVo();
        pageVo.setItems(resultList);
        pageVo.setTotal(0L);
        //分页查询用户充值记录
        IPage<OaDWalletRecord> oacWalletRecordIPage = getUserWalletRecordList(cmsOaDriverWalletListRes.getStartDate(), cmsOaDriverWalletListRes.getEndDate(),
                cmsOaDriverWalletListRes.getRecordType(), cmsOaDriverWalletListRes.getTransactionSerialNo(), driverId, current, pageSize);
        if (CheckUtil.objIsEmpty(oacWalletRecordIPage)) {
            return Rv.wrap(oacWalletRecordIPage);
        }
        OaDUser user = oaDUserMapper.selectById(driverId);
        //返回结果
        OaDUserRechargeListRes result = null;
        for (OaDWalletRecord r : oacWalletRecordIPage.getRecords()) {
            result = new OaDUserRechargeListRes();
            result.setAmount(r.getAmount());
            result.setRecordId(r.getRecordId());
            result.setOrderNo(r.getOrderNo());
            result.setTransactionOrderNo(r.getTransactionOrderNo());
            result.setTransactionSerialNo(r.getTransactionSerialNo());
            if (CheckUtil.objIsNotEmpty(r.getRecordType())) {
                result.setRecordType(ERecordType.findById(r.getRecordType()).getValue());
            }
            if (CheckUtil.objIsNotEmpty(r.getIsTransaction())) {
                result.setIsTransaction(EIsTransaction.findById(r.getIsTransaction()).getDesc());
            }
            result.setTransactionTime(new DateToStringConverter().convert(r.getTransactionTime()));
            result.setDriverName(user.getDriverName());
            resultList.add(result);
        }
        pageVo.setItems(resultList);
        pageVo.setTotal(oacWalletRecordIPage.getTotal());
        return Rv.wrap(EErrorCode.success, pageVo);
    }

    /**
     * 存储司机账户信息
     *
     * @param file
     * @return
     */
    @Override
    public String saveExcelFileContent(Long sysUserId, String tag, String clientId, MultipartFile file) {
        if (CheckUtil.objIsEmpty(file)) {
            throw PangException.create(EErrorCode.missingArg);
        }
        //用poi工具类读取excel文件中的内容
        Workbook workBook = PoiUtil.getWorkBook(file);
        int update = 0;
        try {
            List<List<String>> lists = PoiUtil.readXls(workBook);
            for (int i = 0; i < lists.size(); i++) {
                OaDUserInfo driverUserInfo = new OaDUserInfo();
                List<String> objects = lists.get(i);
                String driverName = objects.get(0);
                String identifyNumber = objects.get(1);
                String account = objects.get(2);
                String proxyUser = objects.get(3);
                String proxyAccount = objects.get(4);
                if (CheckUtil.strIsEmpty(driverName) || CheckUtil.strIsEmpty(identifyNumber)) {
                    throw PangException.create(EErrorCode.impDriverInfoNull);
                }
                if (CheckUtil.strIsEmpty(account) && CheckUtil.strIsEmpty(proxyAccount, proxyUser)) {
                    throw PangException.create(EErrorCode.impDriverAccountNull);
                }
                if((!CheckUtil.strIsEmpty(account) && !Pattern.matches("(^\\d{15,}$)",account))
                            || (!CheckUtil.strIsEmpty(proxyAccount, proxyUser) && !Pattern.matches("(^\\d{15,}$)",proxyAccount))){
                        throw PangException.create(EErrorCode.impDriverAccountError);
                }
                driverUserInfo.setIdentityNumber(identifyNumber);
                driverUserInfo.setBankAccount(account);
                driverUserInfo.setProxyUser(proxyUser);
                driverUserInfo.setProxyAccount(proxyAccount);
                driverUserInfo.setUpdateTime(new Date());
                driverUserInfo.setUpdateUser(sysUserId);
                //将信息插入到oa_driver_info
                UpdateWrapper<OaDUserInfo> driverUserInfoWrapper = new UpdateWrapper<>();
                driverUserInfoWrapper.eq(OaDUserInfo.IDENTITY_NUMBER, driverUserInfo.getIdentityNumber());

                oaDUserInfoMapper.update(driverUserInfo, driverUserInfoWrapper);
            }
            return EErrorCode.impSuccess.getDesc();
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    /**
     * 导出司机账户
     * @param cmsOaDriverWalletListRes
     * @return
     */
    @Override
    public SXSSFWorkbook exportDriverAccountDetail(CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        // 字段名Key
        List<String> fieldNames = new ArrayList<>();
        List<String> sheetHead = new ArrayList<>();
        List<DriverUserResult> driverAccountList = new ArrayList<>();
        if (cmsOaDriverWalletListRes.getExportType() == 0) {
            //设置表头
            fieldNames.add("bankAccountName");
            fieldNames.add("identityNumber");
            fieldNames.add("driverPhone");
            fieldNames.add("name");
            fieldNames.add("bankAccount");
            fieldNames.add("proxyUser");
            fieldNames.add("proxyAccount");
            sheetHead.add("司机姓名");
            sheetHead.add("身份证");
            sheetHead.add("手机");
            sheetHead.add("所属供应商");
            sheetHead.add("招行账户");
            sheetHead.add("代发招行户名");
            sheetHead.add("代发招行账户");
            Long officeId;
            if (cmsOaDriverWalletListRes.getOfficeId() == "" || CheckUtil.strIsEmpty(cmsOaDriverWalletListRes.getOfficeId())) {
                officeId = null;
            } else {
                officeId = Long.valueOf(cmsOaDriverWalletListRes.getOfficeId());
            }
            driverAccountList = oaDUserMapper.exportDriverAccountManageMentList(cmsOaDriverWalletListRes.getDriverPhone(),
                    cmsOaDriverWalletListRes.getBankAccountName(), officeId);
        } else {
            //设置导出模板
            fieldNames.add("bankAccountName");
            fieldNames.add("identityNumber");
            fieldNames.add("bankAccount");
            fieldNames.add("proxyUser");
            fieldNames.add("proxyAccount");
            sheetHead.add("司机姓名");
            sheetHead.add("身份证");
            sheetHead.add("招行账户");
            sheetHead.add("代发招行户名");
            sheetHead.add("代发招行账户");
            DriverUserResult userResult = new DriverUserResult();
            userResult.setBankAccount("6216610200016587010");
            userResult.setIdentityNumber("460104199605110016");
            userResult.setDriverName("张三三");
            userResult.setProxyAccount("6221882600114166800");
            userResult.setProxyUser("李思思");
            driverAccountList.add(userResult);
        }
        SXSSFWorkbook workbook = PoiUtil.export("账户信息", driverAccountList, fieldNames, sheetHead);
        return workbook;
    }

    /**
     * 跟新司机账户
     */
    @Override
    public Rv updateDriverAccountByDriver(OaDUserInfo oaDUserInfo, long sysUserId) {
        if (CheckUtil.objIsEmpty(oaDUserInfo) &&
                CheckUtil.strIsEmpty(oaDUserInfo.getBankAccount()) &&
                CheckUtil.strIsEmpty(oaDUserInfo.getProxyUser(), oaDUserInfo.getProxyAccount())) {
            return Rv.error(EErrorCode.impDriverAccountNull);
        }
        UpdateWrapper<OaDUserInfo> driverUserInfoWrapper = new UpdateWrapper<>();
        oaDUserInfo.setUpdateUser(sysUserId);
        oaDUserInfo.setUpdateTime(new Date());
        driverUserInfoWrapper.eq(OaDUserInfo.IDENTITY_NUMBER, oaDUserInfo.getIdentityNumber());

        int update = oaDUserInfoMapper.update(oaDUserInfo, driverUserInfoWrapper);
        if (update == 0) {
            return Rv.error(EErrorCode.addError);
        } else {
            return Rv.wrap(EErrorCode.success);
        }
    }

    /**
     * 导出详情页面中表格信息
     *
     * @return
     */
    @Override
    public Rv exportGetWalletDetail(CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        String recordType = cmsOaDriverWalletListRes.getRecordType();
        String transactionSerialNo = cmsOaDriverWalletListRes.getTransactionSerialNo();
        Long driverId = cmsOaDriverWalletListRes.getDriverId();
        if (CheckUtil.objIsEmpty(driverId)) {
            throw PangException.create(EErrorCode.missingArg);
        }
        List<OaDUserRechargeListRes> resultList = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc(OaDWalletRecord.CREATE_TIME);
        queryWrapper.eq(OaDWalletRecord.TAG, TokenUtil.getTag());
        queryWrapper.eq(OaDWalletRecord.DRIVER_ID, driverId);
        if (CheckUtil.objIsNotEmpty(transactionSerialNo)) {
            queryWrapper.like(OaDWalletRecord.TRANSACTION_SERIAL_NO, transactionSerialNo);
        }
        if (CheckUtil.objIsNotEmpty(recordType)) {
            queryWrapper.eq(OaDWalletRecord.RECORD_TYPE, recordType);
        }
        if (CheckUtil.objIsNotEmpty(cmsOaDriverWalletListRes.getStartDate())) {
            queryWrapper.ge(OaDWalletRecord.TRANSACTION_TIME, cmsOaDriverWalletListRes.getStartDate());
        }
        if (CheckUtil.objIsNotEmpty(cmsOaDriverWalletListRes.getEndDate())) {
            queryWrapper.le(OaDWalletRecord.TRANSACTION_TIME, cmsOaDriverWalletListRes.getEndDate());
        }
        List<OaDWalletRecord> list = oaDWalletRecordMapper.selectList(queryWrapper);
        if (CheckUtil.objIsEmpty(list)) {
            return Rv.wrap(list);
        }
        OaDUser user = oaDUserMapper.selectById(driverId);
        String driverName = user.getDriverName();
        //返回结果
        OaDUserRechargeListRes result = null;
        for (OaDWalletRecord r : list) {
            result = new OaDUserRechargeListRes();
            result.setAmount(r.getAmount());
            result.setOrderNo(r.getOrderNo());
            result.setTransactionOrderNo(r.getTransactionOrderNo());
            result.setTransactionSerialNo(r.getTransactionSerialNo());
            if (CheckUtil.objIsNotEmpty(r.getRecordType())) {
                result.setRecordType(ERecordType.findById(r.getRecordType()).getValue());
            }
            if (CheckUtil.objIsNotEmpty(r.getIsTransaction())) {
                result.setIsTransaction(EIsTransaction.findById(r.getIsTransaction()).getDesc());
            }
            result.setTransactionTime(new DateToStringConverter().convert(r.getTransactionTime()));
            result.setDriverName(driverName);
            resultList.add(result);
        }
        return Rv.wrap(EErrorCode.success, resultList);
    }

    /**
     * 查询司机钱包记录
     */
    private IPage<OaDWalletRecord> getUserWalletRecordList(Date startDate, Date endDate, String recordType, String transactionSerialNo, Long driverId, Integer page, Integer pageSize) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc(OaDWalletRecord.CREATE_TIME);
        queryWrapper.eq(OaDWalletRecord.TAG, TokenUtil.getTag());
        queryWrapper.eq(OaDWalletRecord.DRIVER_ID, driverId);
        if (CheckUtil.objIsNotEmpty(transactionSerialNo)) {
            queryWrapper.like(OaDWalletRecord.TRANSACTION_SERIAL_NO, transactionSerialNo);
        }
        if (CheckUtil.objIsNotEmpty(recordType)) {
            queryWrapper.eq(OaDWalletRecord.RECORD_TYPE, recordType);
        }
        if (CheckUtil.objIsNotEmpty(startDate)) {
            queryWrapper.ge(OaDWalletRecord.TRANSACTION_TIME, startDate);
        }
        if (CheckUtil.objIsNotEmpty(endDate)) {
            queryWrapper.le(OaDWalletRecord.TRANSACTION_TIME, endDate);
        }
        IPage<OaDWalletRecord> iPage = new Page<>();
        //交易类型
        iPage.setCurrent(page);
        iPage.setSize(pageSize);
        IPage<OaDWalletRecord> list = oaDWalletRecordMapper.selectPage(iPage, queryWrapper);
        return list;
    }

    /**
     * 获取司机周期结算数据
     *
     * @param cmsOaDriverWalletListRes
     * @return
     */
    @Override
    public Rv getSettlementAccountData(CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        //分页查询司机周期结算父级列表
        Integer current = cmsOaDriverWalletListRes.getCurrent();
        Integer pageSize = cmsOaDriverWalletListRes.getPageSize();
        if (CheckUtil.objIsEmpty(current) || CheckUtil.objIsEmpty(pageSize)) {
            throw PangException.create(EErrorCode.missingArg);
        }
        PageDTO dto = new PageDTO();
        dto.setPage(Long.valueOf(current));
        dto.setLimit(Long.valueOf(pageSize));
        IPage iPage = new Page();
        iPage.setCurrent(cmsOaDriverWalletListRes.getCurrent());
        iPage.setPages(cmsOaDriverWalletListRes.getPageSize());
        String startDate = cmsOaDriverWalletListRes.getStartDat();
        String endDate = cmsOaDriverWalletListRes.getEndDat();
        //查询父级列表
        List<CmsDriverSettlementDataListRes> parentData = oaDOrderMapper.getParentSADataList(cmsOaDriverWalletListRes.getOfficeId(), startDate, endDate, dto.getIndex(), dto.getLimit());
        //查询子级列表
        for (CmsDriverSettlementDataListRes dsdl : parentData
        ) {
            Long driverId = dsdl.getDriverId();
            List<CmsDriverSettlementDataResult> sonData = oaDOrderMapper.getSonSADataList(driverId,startDate,endDate);
            dsdl.setCmsDriverSettlementDataResults(sonData);
        }
        PageVo<CmsDriverSettlementDataListRes> listResPageVo = new PageVo<>();
        //条件查询总条数
        Long totalCount = oaDOrderMapper.getParentSADataListCount(cmsOaDriverWalletListRes.getOfficeId(), startDate, endDate);
        listResPageVo.setTotal(totalCount);
        listResPageVo.setItems(parentData);
        return Rv.wrap(EErrorCode.success, listResPageVo);
    }

    @Override
    public SXSSFWorkbook exportDriverSettlementAccountData(CmsOaDriverWalletListRes cmsOaDriverWalletListRes) {
        // 字段名Key
        List<String> fieldNames = new ArrayList<>();
        List<String> sheetHead = new ArrayList<>();
        List<String> sonFieldNames = new ArrayList<>();
        List<String> sonSheetHead = new ArrayList<>();
        List<CmsDriverSettlementDataListRes> driverSettlementList = new ArrayList<>();
        //设置父级表头
        fieldNames.add("driverName");
        fieldNames.add("identityNumber");
        fieldNames.add("driverPhone");
        fieldNames.add("officeName");
        fieldNames.add("totalFee");
        sheetHead.add("司机姓名");
        sheetHead.add("身份证");
        sheetHead.add("电话");
        sheetHead.add("所属供应商");
        sheetHead.add("当期总金额");
        String startDate = cmsOaDriverWalletListRes.getStartDat();
        String endDate = cmsOaDriverWalletListRes.getEndDat();
        //父级列表数据
        long startRunTime = System.currentTimeMillis();
        driverSettlementList = oaDOrderMapper.exportDriverSettlementData(cmsOaDriverWalletListRes.getOfficeId(), startDate, endDate);
        long endRunTime = System.currentTimeMillis();
        System.out.println("父级时间："+(endRunTime - startRunTime));
        //子级列表数据
        sonFieldNames.add("orderNo");
        sonFieldNames.add("driverName");
        sonFieldNames.add("driverPhone");
        sonFieldNames.add("officeName");
        sonFieldNames.add("callType");
        sonFieldNames.add("exchangeTime");
        sonFieldNames.add("totalCost");
        sonFieldNames.add("drawFee");
        sonSheetHead.add("单号");
        sonSheetHead.add("司机姓名");
        sonSheetHead.add("电话");
        sonSheetHead.add("供应商");
        sonSheetHead.add("业务");
        sonSheetHead.add("预约时间");
        sonSheetHead.add("订单总金额");
        sonSheetHead.add("司机分账金额");
        //获取子级列表
        List<CmsDriversettlementExportList> exportSonLists = oaDOrderMapper.exportDriverSettlementSonData(cmsOaDriverWalletListRes.getOfficeId(), startDate, endDate);
        for (CmsDriversettlementExportList cdse:exportSonLists
             ) {
            cdse.setExchangeTime(DateUtil.convertDateToString(cdse.getAppointmentTime()));
            String officeNameById = getOfficeNameById(cdse.getOrderType());
            cdse.setCallType(officeNameById);
        }
        SXSSFWorkbook workbook = new SXSSFWorkbook(8000);
        PoiUtil.exportExcel(workbook, 0, "sheet1", driverSettlementList, fieldNames, sheetHead);
        PoiUtil.exportExcel(workbook, 1, "sheet2", exportSonLists, sonFieldNames, sonSheetHead);
        return workbook;
    }

    /***
     *分页查询司机财务转账金额
     *@param: * @param sysUserId
     *@author:immor
     *@date:2021/5/31
     */
    @Override
    public Rv selectTransferAmountByBatchId(Long batchNo, Integer current, Integer pageSize) {
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPage(Long.valueOf(current));
        pageDTO.setLimit(Long.valueOf(pageSize));
        CmsDTransferRes cmsDTransferRes = new CmsDTransferRes();
        IPage iPage = new Page();
        iPage.setCurrent(current);
        iPage.setSize(pageSize);
        //根据批次号分页查询
        List<CmsDriverSettlementDataListRes> amountList =  oaDDriverCashMapper.selectAmountListByBatchId(batchNo,pageDTO.getIndex(),pageDTO.getLimit());
        //查询总条数
        QueryWrapper<OaDDriverCash> cashQueryWrapper = new QueryWrapper<>();
        cashQueryWrapper.eq(OaDDriverCash.BATCH_ID,batchNo);
        Integer total = oaDDriverCashMapper.selectCount(cashQueryWrapper);
        iPage.setTotal(total);
        iPage.setRecords(amountList);
        // 根据批次号查询该批次总金额
        String totalAmount = oaDDriverCashMapper.selectTotalAmount(batchNo);
        cmsDTransferRes.setIPage(iPage);
        cmsDTransferRes.setTotalAmount(totalAmount);
        return Rv.wrap(cmsDTransferRes);
    }

    private String getOfficeNameById(Integer orderType){
        if(!CheckUtil.objIsEmpty(orderType)){
            switch (orderType){
                case 0:
                    return "实时";
                case 1:
                    return "预约";
                case 2:
                    return "代叫";
                case 3:
                    return "接机";
                case 4:
                    return "送机";
                case 5:
                    return "接站";
                case 6:
                    return "送站";
            }
        }
            return "业务错误";
        }

        /***
        *导入司机财务转账金额
        *@param: * @param sysUserId
         * @param totalAmount
         * @param file
        *@author:immor
        *@date:2021/5/31
        */
    @Override
    public Rv uploadDriverAccountExcel(long sysUserId, BigDecimal totalAmount, MultipartFile file,Long batchNo) {
        if (CheckUtil.objIsEmpty(file)) {
            throw PangException.create(EErrorCode.missingArg);
        }
        if(batchNo == 0){
            batchNo = IDUtil.nextId();
        }
        //用poi工具类读取excel文件中的内容
        Workbook workBook = PoiUtil.getWorkBook(file);
        CmsDriverSettlementDataListRes dataListRes = new CmsDriverSettlementDataListRes();
        String identityNumPattern = "/^\\d{6}(18|19|20)?\\d{2}(0[1-9]|1[012])(0[1-9]|[12]\\d|3[01])\\d{3}(\\d|X)$/i";
        try {
            List<List<String>> lists = PoiUtil.readXls(workBook);
            for (int i = 0; i < lists.size(); i++) {
                long amountID = IDUtil.nextId();
                OaDDriverCash oaDDriverCash = new OaDDriverCash();
                List<String> objects = lists.get(i);
                String driverName = objects.get(0);
                String identifyNumber = objects.get(1);
                String driverPhone = objects.get(2);
                String officeName = objects.get(3);
                String personalTA = objects.get(4);
                if (CheckUtil.strIsEmpty(driverName) || CheckUtil.strIsEmpty(identifyNumber)) {
                    throw PangException.create(EErrorCode.impDriverInfoNull);
                }
                oaDDriverCash.setTrsamt(BigDecimalUtils.value(personalTA));
                oaDDriverCash.setUpdateTime(new Date());
                oaDDriverCash.setUpdateUser(sysUserId);
                //根据身份证查询司机id
                QueryWrapper<OaDUserInfo> userInfoQueryWrapper = new QueryWrapper<>();
                userInfoQueryWrapper.eq(OaDUserInfo.IDENTITY_NUMBER,identifyNumber);
                OaDUserInfo oaDUserInfo = oaDUserInfoMapper.selectOne(userInfoQueryWrapper);

                if(CheckUtil.objIsEmpty(oaDUserInfo) || Pattern.matches(identityNumPattern,oaDUserInfo.getIdentityNumber())){
                    //文件中身份证有误
                    throw PangException.create(EErrorCode.impDriverInfoError);
                }
                Long driverId = oaDUserInfo.getDriverId();
                oaDDriverCash.setDriverId(driverId);
                oaDDriverCash.setRecordType("1");
                //判断银行账户是否有代发人 如果有代发人则填代发人
                if(CheckUtil.objIsNotEmpty(oaDUserInfo.getProxyUser(),oaDUserInfo.getProxyAccount())){
                    oaDDriverCash.setAccnam(oaDUserInfo.getProxyUser());
                    oaDDriverCash.setAccnbr(oaDUserInfo.getProxyAccount());
                }else {
                    oaDDriverCash.setAccnam(oaDUserInfo.getBankAccountName());
                    oaDDriverCash.setAccnbr(oaDUserInfo.getBankAccount());
                }
                // 根据司机id查询该转账对象 如果为空则跟新操作 否则插入
                QueryWrapper<OaDDriverCash> cashQueryWrapper = new QueryWrapper<>();
                cashQueryWrapper.eq(OaDDriverCash.DRIVER_ID,driverId);
                cashQueryWrapper.eq(OaDDriverCash.BATCH_ID,batchNo);
                OaDDriverCash dDriverCash = oaDDriverCashMapper.selectOne(cashQueryWrapper);
                if(CheckUtil.objIsEmpty(dDriverCash)){
                    //新增该转账金额
                    oaDDriverCash.setId(amountID);
                    oaDDriverCash.setCreateUser(sysUserId);
                    oaDDriverCash.setCreateTime(new Date());
                    oaDDriverCash.setBatchId(batchNo);
                    oaDDriverCashMapper.insert(oaDDriverCash);
                }else{
                    //跟新该转账金额
                    UpdateWrapper<OaDDriverCash> cashUpdateWrapper = new UpdateWrapper<>();
                    cashUpdateWrapper.eq(OaDDriverCash.DRIVER_ID,driverId);
                    cashUpdateWrapper.eq(OaDDriverCash.BATCH_ID,batchNo);
                    batchNo = dDriverCash.getBatchId();
                    oaDDriverCashMapper.update(oaDDriverCash,cashUpdateWrapper);
                }
            }
           return Rv.wrap(batchNo);
        } catch (Exception e) {
            e.printStackTrace();
            return Rv.wrap(e.getMessage());
        }
    }


}
